package com.niit.web.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.niit.bean.Admin;
import com.niit.service.AdminService;
import com.niit.service.impl.AdminServiceImpl;
import com.niit.utils.WebKit;

@WebServlet("/AdminLoginController")
public class AdminLoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String verify = request.getParameter("verify").trim().toUpperCase();
		String rand = (String) request.getSession().getAttribute("rand");
		OutputStream out = response.getOutputStream();
		if (verify.equals(rand)) {
			Admin admin = WebKit.request2Bean(request, Admin.class);
			AdminService dao = new AdminServiceImpl();
			if (dao.login(admin)) {
				// 登录成功
				request.getSession().setAttribute("admin", admin);//将登陆过的管理员存入到admin中
				out.write("2".getBytes());
			} else {
				// 用户名或者密码错误
				out.write("1".getBytes());
			}
		}else{
			out.write("0".getBytes());
		}
	}

}
